package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.mapper.AppointmentMapper;
import cn.wolfcode.car.business.query.AppointmentQueryObject;
import cn.wolfcode.car.business.service.IAppointmentService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.PrivilegedAction;
import java.util.Date;
import java.util.List;

/**
 * 服务项目服务impl
 *
 * @author RNIZ
 * @date 2021/11/13
 */
@Service
public class AppointmentServiceImpl implements IAppointmentService {
    
    @Autowired
    private AppointmentMapper appointmentMapper;
    @Autowired
    private StatementServiceImpl statementService;
    @Override
    public TablePageInfo<Appointment> query(AppointmentQueryObject qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<Appointment>(appointmentMapper.selectForList(qo));
    }

    @Override
    public Appointment get(Long id) {
        return appointmentMapper.get(id);
    }


    @Override
    public void save(Appointment appointment) {
        appointment.setDeleteFlag(Appointment.DELETE_EXIST);
        appointmentMapper.insert(appointment);
    }
//    @Override
//    public void deleteBatch(String ids) {
//
//            appointmentMapper.delete(ids);
//
//    }


    @Override
    public void update(Appointment appointment) {
        Appointment oldAppoint = appointmentMapper.get(appointment.getId());
        if (!Appointment.STATUS_APPOINTMENT.equals(oldAppoint.getStatus())){
            throw new BusinessException("先进行预约才能做修改");
        }
        appointmentMapper.update(appointment);
    }

    @Override
    public List<Appointment> list() {
        return appointmentMapper.selectAll();
    }

    @Override
    public void changeStatus(Long id) {
        Appointment oldAppoint = appointmentMapper.get(id);
        if (!Appointment.STATUS_APPOINTMENT.equals(oldAppoint.getStatus())){
            throw new BusinessException("先进行预约才能做取消");
        }
        appointmentMapper.changeStatus(id,Appointment.STATUS_CANCEL);
    }

    @Override
    public void remove(String ids) {
        Long[] array = Convert.toLongArray(ids);
        for (Long aLong : array) {
            Appointment oldAppoint = appointmentMapper.get(aLong);
            if (!Appointment.STATUS_APPOINTMENT.equals(oldAppoint.getStatus())){
                throw new BusinessException("先进行预约才能做删除");
            }
            appointmentMapper.deleteFlag(aLong,Appointment.DELETE_DELETED);
//            appointmentMapper.delete(id);
        }
    }

    @Override
    public void arrival(Long id) {
        Appointment oldAppoint = appointmentMapper.get(id);
        if (!Appointment.STATUS_APPOINTMENT.equals(oldAppoint.getStatus())){
            throw new BusinessException("先进行预约");
        }

        appointmentMapper.arrival(id,Appointment.STATUS_ARRIVAL,new Date());

    }

    @Override
    public void changeByStatus(Long appointmentId, Integer statusSettle) {
        appointmentMapper.changeStatus(appointmentId, statusSettle);
    }

    @Override
    public Statement generateStatement(Long id) {
        Appointment appointment = appointmentMapper.get(id);
        if (!(Appointment.STATUS_ARRIVAL.equals(appointment.getStatus())||Appointment.STATUS_SETTLE.equals(appointment.getStatus()))){
            throw new BusinessException("请先到店");
        }
        Statement statement = statementService.getByAppipontId(id);
        if (statement == null) {
            statement = new Statement();
            /*查询如果付款表单里面没有数据直接进行添加,对下面返回一个id,会直接跳转到明细账单*/
            //名字
            statement.setCustomerName(appointment.getCustomerName());
            //电话
            statement.setCustomerPhone(appointment.getCustomerPhone());
            //到店时间
            statement.setActualArrivalTime(appointment.getActualArrivalTime());
            //车牌号
            statement.setLicensePlate(appointment.getLicensePlate());
            //车的类型
            statement.setCarSeries(appointment.getCarSeries());
            //服务类型
            statement.setServiceType(appointment.getServiceType());
            //用户id
            statement.setAppointmentId(appointment.getId());
            //操作时间
            statement.setCreateTime(new Date());
            //备注信息
            statement.setInfo(appointment.getInfo());

            statementService.save(statement);
        }
        return statement;
    }


}
